// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/base/range.h>

#include <gtest/gtest.h>

namespace kumo::pollux {

TEST(RangeTest, ranges) {
  std::vector<uint64_t> bits(10);
  uint64_t* data = &bits[0];

  Range<bool> readable(data, 11, 511);
  MutableRange<bool> writable(data, 9, 509);
  Range<uint8_t> readableBytes(&bits[0], 1, 79);
  MutableRange<uint8_t> writableBytes(&bits[0], 0, 80);

  // Bit 13 appears as bit 2 in 'readable' and as bit 4 in 'writable'.
  bits::setBit(data, 13);
  EXPECT_TRUE(readable[2]);
  EXPECT_TRUE(writable[4]);

  // Bit 21 appears as bit 11 in 'readable' and as bit 13 in 'writable'.
  writable[13] = true;
  EXPECT_TRUE(readable[11]);

  writable[13] = false;
  EXPECT_FALSE(readable[11]);

  // Byte 10 (ie, bit[80~87]), which corresponds to byte 9 in 'readableBytes'
  // and bit[69~76] in 'readable'.
  writableBytes[10] = 123;
  EXPECT_EQ(readableBytes[9], 123);
  // Bit 80 is set.
  EXPECT_TRUE(readable[69]);
}

} // namespace kumo::pollux
